<?php

class Role extends CI_Model {
 
    protected $permissions;
    
    function __construct() {
        parent::__construct();
        $this->permissions = array();
        }

        
        function insertPerm($desc) {
         $data = array( 'perm_desc' => $desc);   
         $this->db->insert('permission', $data); 
          }
          
          function deletePerm($id) {
         $data = array( 'perm_id' =>$id );   
         $this->db->delete('permission', $data); 
          }
          
          function updatePerm($id, $desc) {
         $data = array( 'perm_id' =>$id );   
         $data ['perm_desc'] =$desc ;
         $this->db->update('permission', $data); 
          }
          
          
          function insertRole($name) {
         $data = array( 'role_name' => $name);   
         $this->db->insert('roles', $data); 
          }
          
          function deleteRole($id) {
         $data = array( 'role_id' =>$id );   
         $this->db->delete('roles', $data); 
          }
          
          function updateRole($id, $name) {
         $data = array( 'role_name' =>$name );  
         $data ['role_id'] =$id ;
         $this->db->update('roles', $data); 
          }
          
           function insertUserRole($userId, $roleId) {
         $data = array( 'utente_id' => $userId);   
          $data ['role_id']= $roleId; 
         $this->db->insert('utente_role', $roleId);
          }
          
          function deleteUserRole($userId, $roleId) {
         $data = array( 'role_id' =>$roleId ); 
         $data ['utente_id'] =$userId ;
         $this->db->delete('utente_role', $data); 
          }
          
          function updateUserRole($userId, $roleId) {
         $data = array( 'utente_id' =>$userId );  
         $data ['role_id'] =$roleId ;
         $this->db->update('utente_role', $data); 
          }
          
          
           function insertRolePerm($permId, $roleId) {
         $data = array( 'perm_id' => $permId);   
          $data ['role_id']= $roleId; 
         $this->db->insert('role_perm', $roleId);
          }
          
          function deleteRolePerm($permId, $roleId) {
         $data = array( 'role_id' =>$roleId ); 
         $data ['perm_id'] =$permId ;
         $this->db->delete('role_perm', $data); 
          }
          
          function updateRolePerm($permId, $roleId) {
         $data = array( 'perm_id' =>$permId );  
         $data ['role_id'] =$roleId ;
         $this->db->update('role_perm', $data); 
          }
          
          
          
          
          
          
          
          
          
           function getUserPerm($userId) {
               
              $sql="SELECT rp.perm_id, r.role_name
                        FROM utente_role AS ur
                        INNER JOIN role_perm AS rp ON ur.role_id = rp.role_id
                        INNER JOIN roles AS r ON r.role_id = ur.role_id
                        WHERE ur.utente_id =".$userId;
                $query = $this->db->query($sql);
             
             if ($query->num_rows() >= 1) {
                 $i=0;
                 foreach ($query->result() as $row) {
                    
                     $result[$i][$row->role_name]=$row->perm_id;//da vedere e controllare se ho due permessi come pet-sitter
                      $i++;                                    //sovrascrivo il primo?? o li memorizziamo sulla stessa riga??
                 }
                 print_r($result);
                 return $result;
        } else {
            return false;
        } 
            }
            
            
         function getRole($perm) {
             
             $role = array_keys($perm[0]);
         
             
             return $role[0];
         } 
         
    }
   


?>
